<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<script type="text/javascript">
    //javascript的乘法结果会有误差，在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
    function accMul(arg1,arg2){

        var m=0,s1=arg1.toString(),s2=arg2.toString();

        try{m+=s1.split(".")[1].length}catch(e){}

        try{m+=s2.split(".")[1].length}catch(e){}

        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)

    }

    /**修改抢购**/
    <shiro:hasPermission name="back_gotoPanicBuyingPage_update">
    function editPanicInfo(){
        var selectedRow= $("#tt").bootstrapTable('getSelections');
        if(selectedRow.length==1){
            var isPublish = selectedRow[0].isPublish;
            var isOver = selectedRow[0].isOver;
            var panicId = selectedRow[0].panicId;
            if(isPublish == 1 && isOver==1){
                $("#myPanicBuyingModalLabel").empty().text("修改抢购商品信息");
                $("#btn_edit").attr("data-target", "#savePanicBuyingModal");/**选中了记录后才可打开对应的模态框**/
                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: "${basePath}/back/panicBuying/getPanicBying.sjson",
                    data: {panicId: panicId},
                    success: function (result) {
                        var data = JSON.parse(result.data);
                        if (data!= null) {
                            $("#panicId").val(data.panicId);
                            $("#productName").val(data.productName);
                            $("#h_productId").val(data.productId);
                            $("#shopInfoId").val(data.shopInfoId);
                            $("#panicTitle").val(data.panicTitle);
                            $("#panicNum").val(data.panicNum);
                            $("#originalPrice").val(data.originalPrice);
                            $("#panicPrice").val(data.panicPrice);
                            $("#startTime").val("");
                            $("#activityTime").val(data.activityTime);
                            $("#imageUrl_1").val(data.logoImg);
                            $("#productId_show").empty().html(data.productId);
                            $("#productName_show").empty().html(data.productName);
                            $("#originalPrice_show").empty().html(data.originalPrice);
                            /**回写预览上传文件所需的信息，如果有上传图片功能添加此代码，否则可以去掉**/
                            initImageForUpdate(1,"sys_shop", "image_promotion", 1, 1000, 300, "${uploadPath}", data.logoImg);
                            reValidateForm();
                            removeValidateField('imageInfo_1');
                            showStartDatePicker(new Date());
                        }
                    }
                });
            }else{
                $("#btn_edit").attr("data-target", "");/**选中了记录后才可打开对应的模态框**/
                showMsg("未结束抢购不可修改！");
            }
        }else{
            $("#btn_edit").attr("data-target", "");/**选中了记录后才可打开对应的模态框**/
            showMsg("请选择一行记录！");
        }
    }
    </shiro:hasPermission>

    /**
     * 自定义验证规则
     */
    function validateForm() {
        form.bootstrapValidator({
            message: "无效的值",
            feedbackIcons: {
                valid: "glyphicon glyphicon-ok",
                invalid: "glyphicon glyphicon-remove",
                validating: "glyphicon glyphicon-refresh"
            },
            fields: {
                imageInfo_1: {
                    validators: {
                        notEmpty: {message: "请上传图片！"},
                        file: {maxSize: 1024 * 1024, message: '请上传一张最大不超过1M的jpg/png/gif图片文件'}
                    }
                },
                panicTitle: {
                    validators: {
                        notEmpty: {message: "抢购标题不可为空"},
                        stringLength: {min:2,max: 100, message: "抢购标题长度为2至100"}
                    }
                },
                startTime: {
                    validators: {
                        notEmpty: {message: "开始时间不可为空"}
                    }
                },
                panicNum: {
                    validators: {
                        notEmpty: {message: "抢购商品总数不能为空"},
                        numeric: {
                            message: '只能输入数字'
                        }
                    }
                },
                activityTime: {
                    validators: {
                        notEmpty: {message: "抢购时长不能为空"},
                        numeric: {
                            message: '只能输入数字'
                        }
                    }
                },
                panicPrice: {
                    validators: {
                        notEmpty: {message: "抢购价格不可为空"},
                        numeric: {
                            message: '只能输入数字'
                        },
                        callback: {message: '抢购价格应大于等于最低比例价，小于原销售价',
                            callback: function(value, validator) {
                                var result = false;
                                var originalPrice = $("#originalPrice").val(); //商品原销售价
                                var panicPrice = $("#panicPrice").val();//抢购价格
                                var minPanicBuyingPriceDiscount=${redis:getSysConfig('minPanicBuyingPriceDiscount')};//获得内存配置表中抢购价最低比例
                                var minPanicBuyingPrice= accMul(originalPrice,minPanicBuyingPriceDiscount);//最小抢购金额  原价乘最低抢购比例获得最低抢购价格
                                if (parseInt(panicPrice) > parseInt(originalPrice)) {
                                    result = false;
                                }else if(parseInt(minPanicBuyingPrice) > parseInt(panicPrice)){
                                    result = false;
                                } else {
                                    result = true;
                                }
                                return result;
                            }
                        }
                    }
                }
            }
        });;
    }

    /**
     * 1、处理时间选择器精确到小时的表单提交
     * 2、在小时后进行分钟，秒:00:00的追加处理
     */
    function submitPanicForm() {
        /**1、对非空时间进行秒的处理**/
        var startTime=$("#startTime").val();
        if(startTime!=""){
            $("#startTime").val(startTime.substring(0,16)+":00");
        }

        submitForm();
    }
</script>

<div class="modal fade" id="savePanicBuyingModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width:70%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h5 class="modal-title" id="myPanicBuyingModalLabel"></h5>
            </div>
            <div class="modal-body">
                <form id="form1" method="post" action="${basePath}/back/panicBuying/addPanicBuying.sjson">
                    <input id="panicId" type="hidden" name="panicId" value="">
                    <input id="h_productId" type="hidden" name="productId" value="">
                    <input id="shopInfoId" type="hidden" name="shopInfoId" value="">
                    <input id="productName" type="hidden" name="productName">
                    <input id="originalPrice" type="hidden" name="originalPrice"/>
                    <table align="center" class="addOrEditTable table-bordered table" width="width:100%;">
                        <tr>
                            <td class="toright_td" width="150px"><span style="color:red">*</span>抢购商品图片:&nbsp;&nbsp;</td>
                            <td class="toleft_td" colspan="3">&nbsp;&nbsp;
                                <div class="container">
                                    <input id="imageUrl_1" type="hidden" name="logoImg" value=""/>
                                    <div class="form-group" id="fileWrap1"></div>
                                </div>
                                <span style="color:red">提示：请上传规格宽1000px，高300px的图片</span>
                            </td>
                        </tr>
                        <tr>
                            <td class="toright_td"  width="150px">商品名称:&nbsp;&nbsp;</td>
                            <td class="toleft_td" colspan="3">&nbsp;&nbsp;
                                <span id="productName_show"></span>
                            </td>
                        </tr>
                        <tr>
                            <td class="toright_td" width="150px"><span style="color:red">*</span>抢购商品标题:&nbsp;&nbsp;</td>
                            <td class="toleft_td" colspan="3">
                                <div class="form-group">
                                    <input id="panicTitle" class="form-control input-sm" type="text" name="panicTitle" placeholder="请输入抢购商品标题"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="toright_td"  width="150px">商品编号:&nbsp;&nbsp;</td>
                            <td class="toleft_td" width="250px">&nbsp;&nbsp;
                                <span id="productId_show"></span>
                            </td>
                            <td class="toright_td" width="150px"><span style="color:red">*</span>抢购商品总数:&nbsp;&nbsp;</td>
                            <td class="toleft_td" width="250px">
                                <div class="form-group">
                                    <input type="text" class="form-control input-sm" name="panicNum" id="panicNum" placeholder="请输入抢购商品总数"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="toright_td">原销售价:&nbsp;&nbsp;</td>
                            <td class="toleft_td">&nbsp;&nbsp;
                                （参考）<span id="originalPrice_show"></span>元
                            </td>
                            <td class="toright_td"><span style="color:red">*</span>抢购价格:&nbsp;&nbsp;</td>
                            <td class="toleft_td">
                                <div class="form-group">
                                    <input id="panicPrice" class="form-control input-sm" type="text" name="panicPrice" placeholder="请输入抢购价格"/>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="toright_td"><span style="color:red">*</span>抢购开始时间:&nbsp;&nbsp;</td>
                            <td class="toleft_td">
                                <div class="form-group">
                                    <input id="startTime" name="startTime" class="form-control input-sm" type="text" value="" placeholder="请选择抢购开始时间" style="cursor: pointer;font-size: 12px;" readonly>
                                </div>
                            </td>
                            <td class="toright_td"><span style="color:red">*</span>抢购活动时长(小时):&nbsp;&nbsp;</td>
                            <td class="toleft_td">
                                <div class="form-group">
                                    <input id="activityTime" class="form-control input-sm" type="text" name="activityTime" placeholder="请输入抢购活动时长(小时)"/>
                                </div>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" id="validateBtn" class="btn btn-primary" onclick="submitPanicForm()">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>